﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Castle.MicroKernel;

namespace DynCMS.NET.Infrastructure.IOC.Windsor.Impl
{
    internal class ControllerFactory : Infrastructure.IOC.MVC.ControllerFactory
    {
        IKernel _kernel;

        public ControllerFactory(IKernel kernel)
        {
            this._kernel = kernel;
        }

        public override void ReleaseController(System.Web.Mvc.IController controller)
        {
            this._kernel.ReleaseComponent(controller);
        }

        protected override System.Web.Mvc.IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
            }

            if (this._kernel.HasComponent(controllerType))
            {
                return this._kernel.Resolve(controllerType) as System.Web.Mvc.IController;
            }
            return base.GetControllerInstance(requestContext, controllerType);
        }
    }
}